const { DEFAULT_EXPIRE_TIME } = require("../config");
const { getDB } = require("../mongo");

// 删除30天前的数据，定时任务也会每天执行一次
async function deleteOldData() {
  const db = getDB();
  const collection = db.collection("tracingList");
  const thirtyDaysAgo = new Date();
  // 获取30天前的时间，并删除
  thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - DEFAULT_EXPIRE_TIME);
  await collection.deleteMany({
    triggerTime: { $lt: thirtyDaysAgo.getTime() },
  });
}

module.exports = deleteOldData;
